package com.sysware.mymodule.controller;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sysware.mymodule.dto.GiteeRepo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.StreamUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.nio.charset.Charset;
import java.util.List;

@RestController
@RequestMapping("/api/v5")
public class ApiGiteeRepo {

    @Autowired
    private ObjectMapper objectMapper;

    ///users/{user}/repos?type=all&sort=full_name&page={start}&per_page={limit}
    @GetMapping("/users/{user}/repos")//?type=all&sort=full_name
    public List<GiteeRepo> getRepoList(@RequestParam("user") String user,
                                       @RequestParam("page") int start,
                                       @RequestParam("per_page") int limit)
            throws Exception {
        ClassPathResource classPathResource = new ClassPathResource("data/a.json");
        String data = StreamUtils.copyToString(classPathResource.getInputStream(), Charset.defaultCharset());

        List<GiteeRepo> list = objectMapper.readValue(data, new TypeReference<List<GiteeRepo>>(){});
        return list;
    }

}
